package com.duanjw.bank.rate.handler;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.duanjw.bank.rate.core.BankRate;
import com.duanjw.bank.rate.core.BankRateHandler;
import com.duanjw.bank.rate.core.DateFormatUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;

/**
 * 深圳农商行
 *
 * @author duanjw
 * @date 2024/4/1
 */
public class ShenZhenNongShangBanRateHandler implements BankRateHandler {
    @Override
    public BankRate getBankRate() throws IOException {
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=utf-8");
        headers.put("Origin", "https://www.4001961200.com");
        headers.put("Referer", "https://www.4001961200.com/deposit-interest-rate");
        headers.put("Accept-Language", "zh-CN");
        headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36"); // 仿真
        Connection connect = Jsoup.connect("https://www.4001961200.com/api/fromInternet/SaveRateQry.do?ClientType=Client");
        Connection.Response execute = connect.headers(headers).ignoreContentType(true).method(Connection.Method.POST).execute();

        JSONObject responseDoc = new JSONObject(execute.body());
        JSONArray rmbRateList = responseDoc.getJSONArray("RMBRateList");

        String kaiShiZhiXingRiQi = responseDoc.getStr("RMBRateListBeginDate");
        String huoQiCunKuanLiLv = rmbRateList.getJSONObject(0).getStr("Rate").replace("%", "");

        String zhengCunZhengQu3YueLiLv = rmbRateList.getJSONObject(1).getStr("Rate").replace("%", "");
        String zhengCunZhengQu6YueLiLv = rmbRateList.getJSONObject(2).getStr("Rate").replace("%", "");
        String zhengCunZhengQu12YueLiLv = rmbRateList.getJSONObject(3).getStr("Rate").replace("%", "");
        String zhengCunZhengQu24YueLiLv = rmbRateList.getJSONObject(4).getStr("Rate").replace("%", "");
        String zhengCunZhengQu36YueLiLv = rmbRateList.getJSONObject(5).getStr("Rate").replace("%", "");
        String zhengCunZhengQu60YueLiLv = rmbRateList.getJSONObject(6).getStr("Rate").replace("%", "");

        String lingCunZhengQu12YueLiLv = rmbRateList.getJSONObject(9).getStr("Rate").replace("%", "");
        String lingCunZhengQu36YueLiLv = rmbRateList.getJSONObject(10).getStr("Rate").replace("%", "");
        String lingCunZhengQu60YueLiLv = rmbRateList.getJSONObject(11).getStr("Rate").replace("%", "");

        String dingHuoLiangBian3YueLiLv = new BigDecimal(zhengCunZhengQu3YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        String dingHuoLiangBian6YueLiLv = new BigDecimal(zhengCunZhengQu6YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();
        String dingHuoLiangBian12YueLiLv = new BigDecimal(zhengCunZhengQu12YueLiLv).multiply(new BigDecimal("0.6")).setScale(3, RoundingMode.HALF_UP).toString();

        String tongZhiCunKuan1TianLiLv = rmbRateList.getJSONObject(7).getStr("Rate").replace("%", "");
        String tongZhiCunKuan7TianLiLv = rmbRateList.getJSONObject(8).getStr("Rate").replace("%", "");

        String xieDingCunKuanLiLv = rmbRateList.getJSONObject(12).getStr("Rate").replace("%", "");

        return new BankRate(DateFormatUtil.format2(kaiShiZhiXingRiQi), huoQiCunKuanLiLv, zhengCunZhengQu3YueLiLv, zhengCunZhengQu6YueLiLv, zhengCunZhengQu12YueLiLv, zhengCunZhengQu24YueLiLv, zhengCunZhengQu36YueLiLv, zhengCunZhengQu60YueLiLv, lingCunZhengQu12YueLiLv, lingCunZhengQu36YueLiLv, lingCunZhengQu60YueLiLv, dingHuoLiangBian3YueLiLv, dingHuoLiangBian6YueLiLv, dingHuoLiangBian12YueLiLv, tongZhiCunKuan1TianLiLv, tongZhiCunKuan7TianLiLv, getXieDingCunKuanLiLv(xieDingCunKuanLiLv));
    }

    @Override
    public String getBankName() {
        return "深圳农商行";
    }

    private String getXieDingCunKuanLiLv(String xieDingCunKuanLiLv) {
        if (StrUtil.isBlank(xieDingCunKuanLiLv)) {
            return null;
        }
        return xieDingCunKuanLiLv;
    }
}
